package dk.aau.walkietalkie.database;

import java.util.ArrayList;
import java.util.HashMap;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.provider.BaseColumns;
import android.util.Log;

public class DBController extends SQLiteOpenHelper {

	private static final String LOGCAT = null;
	public static final String DATABASE_NAME = "walkie_talkie_aau.db";
	public static final int DATABASE_VERSION = 3;

	public static abstract class DBControllerEntry implements BaseColumns {
		public static final String TABLE_NAME = "IPAdresses";
		public static final String COLUMN_NAME_IP = "IP";
		public static final String COLUMN_NAME_NAME_USER = "Name";
		public static final String COLUMN_NAME_ALIVE_COUNTER = "Alive";

	}

	// DATABASE FILE NAME
	public DBController(Context applicationcontext) {
		super(applicationcontext, DATABASE_NAME, null, DATABASE_VERSION);
		Log.d(LOGCAT, "Created");
	}

	// Creation of the table
	@Override
	public void onCreate(SQLiteDatabase database) {
		String query;
		query = "CREATE TABLE " + DBControllerEntry.TABLE_NAME + "("
				+ DBControllerEntry.COLUMN_NAME_IP + " TEXT PRIMARY KEY,"
				+ DBControllerEntry.COLUMN_NAME_NAME_USER + " TEXT,"
				+ DBControllerEntry.COLUMN_NAME_ALIVE_COUNTER + " INTEGER" + ")";
		database.execSQL(query);
		Log.d(LOGCAT, "IPAdresses Created");
	}

	@Override
	public void onUpgrade(SQLiteDatabase database, int version_old,
			int current_version) {
		String query;
		query = "DROP TABLE IF EXISTS " + DBControllerEntry.TABLE_NAME + "";
		database.execSQL(query);
		onCreate(database);
	}

	// Insertion data into the table
	// TODO : WORKS
	public long insertData(HashMap<String, String> queryValues) {
		long result = -1;
		SQLiteDatabase database = this.getWritableDatabase();
		ContentValues values = new ContentValues();
		values.put(DBControllerEntry.COLUMN_NAME_IP, queryValues.get("IP"));
		values.put(DBControllerEntry.COLUMN_NAME_NAME_USER,
				queryValues.get("Name"));
		values.put(DBControllerEntry.COLUMN_NAME_ALIVE_COUNTER, 0);
		result = database.insert(DBControllerEntry.TABLE_NAME, null, values);

		database.close();
		Log.d(LOGCAT, "Data_inserted, result: " + result);
		return result;

	}

	//

	// Update a specific row
	// TODO : WORKS but not necessary to use this function in our case
	public long updateRow(Identification id) {
		long result = 0;
		SQLiteDatabase database = this.getWritableDatabase();
		ContentValues values = new ContentValues();
		values.put(DBControllerEntry.COLUMN_NAME_NAME_USER, id.getName());
		values.put(DBControllerEntry.COLUMN_NAME_ALIVE_COUNTER,
				id.getAliveCounter());
		result = database.update(DBControllerEntry.TABLE_NAME, values,
				DBControllerEntry.COLUMN_NAME_IP + " = ?",
				new String[] { id.getIp() });
		if (result == 0) {
			HashMap<String, String> newEntry = new HashMap<String, String>();
			newEntry.put("Name", id.getName());
			newEntry.put("IP", id.getIp().toString());
			result = insertData(newEntry);
		}

		Log.d(LOGCAT, "Update " + DBControllerEntry.TABLE_NAME + "with result "
				+ result);
		return result;

	}

	// Delete a specific row
	// TODO : WORKS
	public void deleteRow(String IP) {
		Log.d(LOGCAT, "delete");
		SQLiteDatabase database = this.getWritableDatabase();
		String deleteQuery = "DELETE FROM  " + DBControllerEntry.TABLE_NAME
				+ " where " + DBControllerEntry.COLUMN_NAME_IP + "='" + IP
				+ "'";
		Log.d("query", deleteQuery);
		database.execSQL(deleteQuery);
	}

	public void deleteTable() {
		Log.d(LOGCAT, "delete");
		SQLiteDatabase database = this.getWritableDatabase();
		String deleteQuery = "DELETE FROM  " + DBControllerEntry.TABLE_NAME;
		Log.d("query", deleteQuery);
		database.execSQL(deleteQuery);
		database.close();
	}

	// Show all the data from the table
	public ArrayList<Identification> getAllContacts() {
		ArrayList<Identification> contactList = new ArrayList<Identification>();
		String selectQuery = "SELECT  * FROM " + DBControllerEntry.TABLE_NAME;
		SQLiteDatabase database = this.getWritableDatabase();
		Cursor c = database.rawQuery(selectQuery, null);
		if (c.moveToFirst()) {
			do {
				Identification id = new Identification(c.getString(0) /* IP */,
						c.getString(1) /* Name */, c.getInt(2)/* AliveCounter */);
				contactList.add(id);
			} while (c.moveToNext());
		}

		// return contact list
		return contactList;
	}

	// Show specific data from the table
	public HashMap<String, String> getContactName(String IP) {
		HashMap<String, String> wordList = new HashMap<String, String>();
		SQLiteDatabase database = this.getReadableDatabase();
		String selectQuery = "SELECT * FROM " + DBControllerEntry.TABLE_NAME
				+ " where " + DBControllerEntry.COLUMN_NAME_IP + "='" + IP
				+ "'";
		Cursor cursor = database.rawQuery(selectQuery, null);
		if (cursor.moveToFirst()) {
			do {
				// HashMap<String, String> map = new HashMap<String, String>();
				wordList.put(DBControllerEntry.COLUMN_NAME_NAME_USER,
						cursor.getString(1));
				// wordList.add(map);
			} while (cursor.moveToNext());
		}
		return wordList;
	}
}